package  com.diamond.action;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.diamond.bo.InlayDiaPicBO;
import com.diamond.bo.InlayDiamonBO;
import com.diamond.bo.QueryInlayDiamondBO;
import com.diamond.bo.ReturnBO;
import com.diamond.entity.TInlayDiaPic;
import com.diamond.facade.InlayDiamondFacade;
import com.diamond.util.JsonMSG;
import com.diamond.util.JsonUtils;
import com.diamond.util.StringUtil;
import com.diamond.util.UploadUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * RangAction ������
 * @author JSmart Tools
 */
@SuppressWarnings("unchecked")
public class InlayDiamondAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8257335938771614401L;
	private HttpServletRequest request = ServletActionContext.getRequest();
//	private IRangService rangService;
	private InlayDiamondFacade inlayDiamondFacade;
	private InlayDiamonBO inlayDiamondBo;
	private List dataList;
	private String tip;
	private String json;
	private String inlayDiamondStr;
	private String inlayRate;
	
	private File picture;
	private String pictureFileName;
	private File picture1;
	private String picture1FileName;
	private File picture2;
	private String picture2FileName;
	private File picture3;
	private String picture3FileName;
	private File picture4;
	private String picture4FileName;
	private File picture5;
	private String picture5FileName;
	private File picture6;
	private String picture6FileName;
	private File picture7;
	private String picture7FileName;
	private File picture8;
	private String picture8FileName;

//----------------------后端钻托管理-------------------------------------		
	/**
	 * 上传钻托图片
	 * @return
	 */
	public String uploadInlayDiamond() {
//		HttpServletRequest request= ServletActionContext.getRequest();
//		String pictureFileName =request.getParameter("picFileName");
		Map<String, Object> session = ActionContext.getContext().getSession();
		String userForder="rang/"+(session.get("USER_ID")==null?"default":session.get("USER_ID").toString());
		TInlayDiaPic tInlayDiaPic = inlayDiamondFacade.upLoadOtherPic(picture, userForder, pictureFileName);
		if (StringUtils.isEmpty(tInlayDiaPic.getPicLine())) {
			this.json=JsonMSG.failed("上传失败！").toJson();
		} else {
			this.json=JsonMSG.sucess(tInlayDiaPic).toJson();
		}
		return "json";
	}
	
	/**
	 * 保存钻托数据
	 * @return
	 */
	public String saveInlayDiamond() {
		String str=ServletActionContext.getRequest().getParameter("inlayDiamondStr");
		inlayDiamondBo=(InlayDiamonBO) JsonUtils.toObject(str, InlayDiamonBO.class);
		String inlayDiaPicBoStrs =  ServletActionContext.getRequest().getParameter("InlayDiaPicBoStrs");
		List<InlayDiaPicBO> inlayDiaPicBolist = new ArrayList<InlayDiaPicBO>();
		if (!StringUtil.isEmpty(inlayDiaPicBoStrs)) {
			inlayDiaPicBolist= (List<InlayDiaPicBO>) JSONArray.toCollection(JSONArray.fromObject(inlayDiaPicBoStrs), InlayDiaPicBO.class);
		}
		for (InlayDiaPicBO inlayDiaPicBo : inlayDiaPicBolist) {
			inlayDiaPicBo.setPicLine(inlayDiaPicBo.getPicLine().split(UploadUtil.getPicWeb()+"/")[1]);
			if ("true".equals(inlayDiaPicBo.getIsMainPic())) {
				inlayDiamondBo.setPicture(inlayDiaPicBo.getPicLine());
			}
		}
		
		inlayDiamondBo.setInlayLevel("A");
		inlayDiamondBo.setUserId(1l);
		Boolean result = inlayDiamondFacade.saveInlayDiamond(inlayDiamondBo, inlayDiaPicBolist);
		if (result) {
			this.json=JsonMSG.sucess("钻托数据提交成功！").toJson();
		} else {
			this.json=JsonMSG.failed("钻托数据提交失败！").toJson();
		}
		return "json";
	}
	/**
	 * 查询钻托列表
	 * @return
	 */
	public String queryInlayDiamondList() {
		request = ServletActionContext.getRequest();
		String inlayDiaPriFr = request.getParameter("inlayDiaPriFr");
		String inlayDiaPriTo = request.getParameter("inlayDiaPriTo");
		String inlayDiaName = request.getParameter("inlayDiaName");
		String inlayDiaType = request.getParameter("inlayDiaType");
		String inlayDiaCode = request.getParameter("inlayDiaCode");
		String start = request.getParameter("start");
		String pageSize = request.getParameter("limit");
		QueryInlayDiamondBO queryInlayDiamondBo = new QueryInlayDiamondBO();
		queryInlayDiamondBo.setInlayDiaPriFr(inlayDiaPriFr);
		queryInlayDiamondBo.setInlayDiaPriTo(inlayDiaPriTo);
		queryInlayDiamondBo.setInlayDiaName(inlayDiaName);
		queryInlayDiamondBo.setInlayDiaType(inlayDiaType);
		queryInlayDiamondBo.setInlayDiaCode(inlayDiaCode);
		if (null == start || "".equals(start) || null == pageSize || "".equals(pageSize)) {
			queryInlayDiamondBo.setStart(0);
			queryInlayDiamondBo.setPageSize(100);
		} else {
			queryInlayDiamondBo.setStart(Integer.valueOf(start));
			queryInlayDiamondBo.setPageSize(Integer.valueOf(pageSize));
		}
		try {
			ReturnBO<InlayDiamonBO>	returnlist = inlayDiamondFacade.queryInlayDiamondList(queryInlayDiamondBo);
			this.json=JsonMSG.sucess(returnlist).toJson();
		} catch (Exception e) {
			e.printStackTrace();
			this.json=JsonMSG.failed("钻托查询失败！").toJson();
		}
		return "json";
	}
	
	/**
	 * 查询钻托明细
	 * @return
	 */
	public String queryInlayDiamondDetail() {
		request = ServletActionContext.getRequest();
		String inlayDiaId = request.getParameter("inlayDiaId");
		try {
			InlayDiamonBO inlayDiamondBo = inlayDiamondFacade.queryInlayDiamondDetail(inlayDiaId);
			this.json=JsonMSG.sucess(inlayDiamondBo).toJson();
		} catch (Exception e) {
			e.printStackTrace();
			this.json=JsonMSG.failed("钻托查询失败！").toJson();
		}
		return "json";
	}
	
	/**
	 * 查询钻托图片
	 * @return
	 */
	public String queryInlayDiamondPic() {
		request = ServletActionContext.getRequest();
		String inlayDiaId = request.getParameter("inlayDiaId");
		try {
			List<InlayDiaPicBO> inlayPiclist = inlayDiamondFacade.queryInlayDiamondPic(inlayDiaId);
			this.json=JsonMSG.sucess(inlayPiclist).toJson();
		} catch (Exception e) {
			e.printStackTrace();
			this.json=JsonMSG.failed("钻托查询失败！").toJson();
		}
		return "json";
	}
	
	/**
	 * 修改更新钻托
	 * @return
	 */
	public String updateInlayDiamond() {
		inlayDiamondBo=(InlayDiamonBO) JsonUtils.toObject(inlayDiamondStr, InlayDiamonBO.class);
		try {
			Boolean result = inlayDiamondFacade.updateInlayDiamond(inlayDiamondBo);
			if (result) {
				this.json=JsonMSG.sucess("钻托数据修改成功！").toJson();
			} else {
				this.json=JsonMSG.failed("钻托数据修改失败！").toJson();
			}
		} catch (Exception e) {
			e.printStackTrace();
			this.json=JsonMSG.failed("钻托数据修改失败！").toJson();
		}
		return "json";
	}
	
	/**
	 * 删除钻托
	 * @return
	 */
	public String delInlayDiamond() {
		request = ServletActionContext.getRequest();
		String[] inlayDiaIds = request.getParameter("inlayDiaIds").split(";");
//		Map session = ActionContext.getContext().getSession();
//		String userId= session.get("USER_ID").toString();
		for (String inlayDiaId : inlayDiaIds) {
			inlayDiamondFacade.delInlayDiamond(inlayDiaId);
		}
		this.json=JsonMSG.sucess("删除成功！").toJson();
		return "json";
	}
	
	
	
//----------------------前端钻托管理-------------------------------------		
	/**
	 * 分页查询钻托列表
	 * @return
	 */
	public String toInlay(){
		request = ServletActionContext.getRequest();
		String inlayType = request.getParameter("type");
		if (!StringUtil.isEmpty(inlayType) && StringUtil.isEmpty(inlayDiamondBo)) {
			inlayDiamondBo = new InlayDiamonBO();
			if ("A".equals(inlayType)) {
				inlayDiamondBo.setInlayType("女戒");
			} else if ("B".equals(inlayType)) {
				inlayDiamondBo.setInlayType("男戒");
			} else if ("C".equals(inlayType)) {
				inlayDiamondBo.setInlayType("吊坠");
			} else if ("D".equals(inlayType)) {
				inlayDiamondBo.setInlayType("耳钉");
			} else if ("E".equals(inlayType)) {
				inlayDiamondBo.setInlayType("手链");
			}
		}
		dataList = inlayDiamondFacade.listInlay(ServletActionContext.getRequest(),
				ActionContext.getContext().getSession(),inlayDiamondBo);
		return SUCCESS;
	}
	
//	/**
//	 * 钻托分类查询
//	 * @return
//	 */
//	public String findInlayToType() {
//		request = ServletActionContext.getRequest();
//		String inlayType = request.getParameter("type");
////		System.out.println(inlayType);
////		dataList = inlayDimaondFacade.
//		return SUCCESS;
//	}
	/**
	 * 转到钻托明细
	 * @return
	 */
	public String toInlayDetail(){
		request = ServletActionContext.getRequest();
		Map session = ActionContext.getContext().getSession();
		Object[] obj= inlayDiamondFacade.toInlayDetail(request,session);
		inlayDiamondBo = (InlayDiamonBO) obj[0];
		dataList = (List) obj[1];
		return SUCCESS;
	}
	/**
	 * 转到查看自己的钻托页面
	 * @return
	 */
	public String toMyInlayDia(){
		HttpServletRequest request=ServletActionContext.getRequest();
		Map session = ActionContext.getContext().getSession();
		Object[] objs=inlayDiamondFacade.toMyInlayDia(request,session);
		dataList = (List) objs[0];
		inlayRate = (String) objs[1];
		request.setAttribute("inlayType", 1);
		return SUCCESS;
	}
	
	/**
	 * 转到查看供应商的钻托页面
	 * @return
	 */
	public String toSupInlayDia(){
		HttpServletRequest request=ServletActionContext.getRequest();
		Map session = ActionContext.getContext().getSession();
		Object[] objs=inlayDiamondFacade.toSupInlayDia(request,session);
		if(objs!=null){
			dataList = (List) objs[0];
			inlayRate = (String) objs[1];
		}
		request.setAttribute("inlayType", 2);
		return SUCCESS;
	}
	
	/**
	 * 修改钻托价格系数
	 * @return
	 */
	public String editInlayRate(){
		Map session = ActionContext.getContext().getSession();
		String inlayRate=ServletActionContext.getRequest().getParameter("inlayRate");
		inlayDiamondFacade.editRate(session.get("USER_ID"),inlayRate);
		return SUCCESS;
	}
	
	/**
	 * 修改钻托价格系数
	 * @return
	 */
	public String delInlayDia(){
		Map session = ActionContext.getContext().getSession();
		Object userId=session.get("USER_ID");
		if(userId==null)return SUCCESS;
		String inlayDiaId=ServletActionContext.getRequest().getParameter("inlayDiaId");
		inlayDiamondFacade.delInlayDia(session.get("USER_ID"),inlayDiaId);
		return SUCCESS;
	}
	
	/**
	 * 上传钻托处理
	 * @return
	 */
	public String upInlayDia(){
		Map session = ActionContext.getContext().getSession();
		Long userid=(Long)session.get("USER_ID");
		List<TInlayDiaPic> picList = new ArrayList<TInlayDiaPic>();
		String userForder="rang/"+userid;
		if(!StringUtil.isEmpty(pictureFileName)){
			inlayDiamondFacade.upLoadMainPic(picture,userForder,pictureFileName);
			inlayDiamondBo.setPicture(userForder+"/"+pictureFileName);
		}
		if(!StringUtil.isEmpty(picture1FileName)){
			TInlayDiaPic inlayDiaPic=inlayDiamondFacade.upLoadOtherPic(picture1,userForder,picture1FileName);
			picList.add(inlayDiaPic);
		}
		if(!StringUtil.isEmpty(picture2FileName)){
			TInlayDiaPic inlayDiaPic=inlayDiamondFacade.upLoadOtherPic(picture2,userForder,picture2FileName);
			picList.add(inlayDiaPic);
		}
		if(!StringUtil.isEmpty(picture3FileName)){
			TInlayDiaPic inlayDiaPic=inlayDiamondFacade.upLoadOtherPic(picture3,userForder,picture3FileName);
			picList.add(inlayDiaPic);
		}
		if(!StringUtil.isEmpty(picture4FileName)){
			TInlayDiaPic inlayDiaPic=inlayDiamondFacade.upLoadOtherPic(picture4,userForder,picture4FileName);
			picList.add(inlayDiaPic);
		}
		if(!StringUtil.isEmpty(picture5FileName)){
			TInlayDiaPic inlayDiaPic=inlayDiamondFacade.upLoadOtherPic(picture5,userForder,picture5FileName);
			picList.add(inlayDiaPic);
		}
		if(!StringUtil.isEmpty(picture6FileName)){
			TInlayDiaPic inlayDiaPic=inlayDiamondFacade.upLoadOtherPic(picture6,userForder,picture6FileName);
			picList.add(inlayDiaPic);
		}
		if(!StringUtil.isEmpty(picture7FileName)){
			TInlayDiaPic inlayDiaPic=inlayDiamondFacade.upLoadOtherPic(picture7,userForder,picture7FileName);
			picList.add(inlayDiaPic);
		}
		if(!StringUtil.isEmpty(picture8FileName)){
			TInlayDiaPic inlayDiaPic=inlayDiamondFacade.upLoadOtherPic(picture8,userForder,picture8FileName);
			picList.add(inlayDiaPic);
		}
		
		inlayDiamondBo.setUserId((Long)session.get("USER_ID"));
		inlayDiamondBo.setInlayLevel((String) session.get("USER_TYPE"));
		inlayDiamondFacade.save(inlayDiamondBo,picList);
		tip="钻托上传成功";
		return SUCCESS;
	}
	
	
//---------------------------------------------------------------------------	
	public List getDataList() {
		return dataList;
	}

	public void setDataList(List dataList) {
		this.dataList = dataList;
	}
 
	public InlayDiamonBO getinlayDiamondBo() {
		return inlayDiamondBo;
	}

	public void setinlayDiamondBo(InlayDiamonBO inlayDiamondBo) {
		this.inlayDiamondBo = inlayDiamondBo;
	}

	
	public void setInlayDiamondFacade(InlayDiamondFacade inlayDiamondFacade) {
		this.inlayDiamondFacade = inlayDiamondFacade;
	}
	public InlayDiamonBO getInlayDiamondBo() {
		return inlayDiamondBo;
	}
	public void setInlayDiamondBo(InlayDiamonBO inlayDiamondBo) {
		this.inlayDiamondBo = inlayDiamondBo;
	}
	public String getPictureFileName() {
		return pictureFileName;
	}
	public void setPictureFileName(String pictureFileName) {
		this.pictureFileName = pictureFileName;
	}
	public File getPicture() {
		return picture;
	}
	public void setPicture(File picture) {
		this.picture = picture;
	}
	public String getTip() {
		return tip;
	}
	public void setTip(String tip) {
		this.tip = tip;
	}
	public File getPicture1() {
		return picture1;
	}
	public void setPicture1(File picture1) {
		this.picture1 = picture1;
	}
	public String getPicture1FileName() {
		return picture1FileName;
	}
	public void setPicture1FileName(String picture1FileName) {
		this.picture1FileName = picture1FileName;
	}
	public File getPicture2() {
		return picture2;
	}
	public void setPicture2(File picture2) {
		this.picture2 = picture2;
	}
	public String getPicture2FileName() {
		return picture2FileName;
	}
	public void setPicture2FileName(String picture2FileName) {
		this.picture2FileName = picture2FileName;
	}
	public File getPicture3() {
		return picture3;
	}
	public void setPicture3(File picture3) {
		this.picture3 = picture3;
	}
	public String getPicture3FileName() {
		return picture3FileName;
	}
	public void setPicture3FileName(String picture3FileName) {
		this.picture3FileName = picture3FileName;
	}
	public File getPicture4() {
		return picture4;
	}
	public void setPicture4(File picture4) {
		this.picture4 = picture4;
	}
	public String getPicture4FileName() {
		return picture4FileName;
	}
	public void setPicture4FileName(String picture4FileName) {
		this.picture4FileName = picture4FileName;
	}
	public File getPicture5() {
		return picture5;
	}
	public void setPicture5(File picture5) {
		this.picture5 = picture5;
	}
	public String getPicture5FileName() {
		return picture5FileName;
	}
	public void setPicture5FileName(String picture5FileName) {
		this.picture5FileName = picture5FileName;
	}
	public File getPicture6() {
		return picture6;
	}
	public void setPicture6(File picture6) {
		this.picture6 = picture6;
	}
	public String getPicture6FileName() {
		return picture6FileName;
	}
	public void setPicture6FileName(String picture6FileName) {
		this.picture6FileName = picture6FileName;
	}
	public File getPicture7() {
		return picture7;
	}
	public void setPicture7(File picture7) {
		this.picture7 = picture7;
	}
	public String getPicture7FileName() {
		return picture7FileName;
	}
	public void setPicture7FileName(String picture7FileName) {
		this.picture7FileName = picture7FileName;
	}
	public File getPicture8() {
		return picture8;
	}
	public void setPicture8(File picture8) {
		this.picture8 = picture8;
	}
	public String getPicture8FileName() {
		return picture8FileName;
	}
	public void setPicture8FileName(String picture8FileName) {
		this.picture8FileName = picture8FileName;
	}
	public String getJson() {
		return json;
	}
	public void setJson(String json) {
		this.json = json;
	}
	public String getInlayRate() {
		return inlayRate;
	}
	public void setInlayRate(String inlayRate) {
		this.inlayRate = inlayRate;
	}

	public String getInlayDiamondStr() {
		return inlayDiamondStr;
	}

	public void setInlayDiamondStr(String inlayDiamondStr) {
		this.inlayDiamondStr = inlayDiamondStr;
	}
}
